/*---------------------------------------------------------------------------------------------
 *  Copyright (c) kylinIDETeam. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
const fs = require('fs');
const path = require('path');
const logger = require('../page/view/logger');


function setLanguage(key, ...args) {
	let lan = JSON.parse(process.env.VSCODE_NLS_CONFIG || `{locale: 'zh-cn'}`).locale.toLowerCase();
	// console.log("setLanguage:" + lan);

	let language;
	if (lan === 'zh-cn') {
		language = require('../locale/zh.json');
	} else {
		language = require('../locale/zh.json');
	}
	let res = key;
	if (language[key]) {
		res = language[key];

	}
	if (args.length > 0) {
		args.forEach((arg, index) => {
			let target = `$${index + 1}`;
			res = res.replace(target, arg);
		});
	}
	return res;
}

function setLanguage2(key, ...args) {
	let language = require('../locale/locale.json');
	let lan = JSON.parse(process.env.VSCODE_NLS_CONFIG || `{locale: 'zh-cn'}`).locale.toLowerCase();

	let res = key;
	if (language[key]) {
		// res = lan === 'zh-cn' ? language[key].chinese : language[key].english;
		res = language[key].chinese;//目前仅仅支持中文

		if (typeof res === 'string') {
			let temp = document.createElement('div');
			temp.textContent = res;
			res = temp.innerHTML;
		}
	}
	if (args.length > 0) {
		args.forEach((arg, index) => {
			let target = `$${index + 1}`;
			res = res.replace(target, arg);
		});
	}
	return res;
}

function getLanguage(key, ...args) {
	return "zh-cn";
	// let lan = JSON.parse(process.env.VSCODE_NLS_CONFIG || `{locale: 'zh-cn'}`).locale.toLowerCase();
	// return lan;
}

function sanitizeHtml(str) {
	let temp = document.createElement('div');
	temp.textContent = str;
	return temp.innerHTML;
}

function show(ele) {
	if (arguments) {
		for (let i = 0; i < arguments.length; i++) {
			if (arguments[i]) {
				arguments[i].classList.remove('hide');
			}
		}
	}
}

function hide(ele) {
	if (arguments) {
		for (let i = 0; i < arguments.length; i++) {
			if (arguments[i]) {
				arguments[i].classList.add('hide');
			}
		}
	}

}

function setDarkTheme(streamArgs) {
	logger.info("设置主题" + streamArgs.theme);
	if (streamArgs.theme && streamArgs.theme.indexOf('Dark') !== -1) {
		document.body.classList.add('dark');
	} else {
		document.body.classList.remove('dark');
	}
}
//检查网络是否联通
function checkhttp() {
	return new Promise((resolve, reject) => {
		const options = {
			host: 'www.baidu.com',
			port: 80,
			path: '/',
		};
		const http = require('http');
		const req = http.get(options, (res) => {
			if (res.statusCode === 200) {
				resolve(200);
				logger.info("网络联通正常");
			} else {
				reject(res.statusCode)
				logger.info('无法访问网络');
			}
		});

		req.setTimeout(5000, () => {
			reject(-1);
			logger.error('网络请求验证超时');
		});
		req.on('error', (error) => {
			reject(error);
			logger.error('网络链接发生错误:', error);
		});
		req.end();
	})
}

//获取全局变量g_index.json下载网络
function get_g_index_url() {
	let { gIndexUrl } = require('../../../../../../product.json');
	return gIndexUrl;
}

function configDir() {
	// 获取用户目录
	const userHome = os.homedir();
	// 创建日志目录
	const configDir = `${userHome}/.config/Kylin-Code/installconfig`;
	if (!fs.existsSync(configDir)) {
		fs.mkdirSync(configDir, { recursive: true }); // 递归创建目录
	}
	return configDir;
}



exports.setLanguage = setLanguage;
exports.show = show;
exports.hide = hide;
exports.setDarkTheme = setDarkTheme;
exports.sanitizeHtml = sanitizeHtml;
exports.checkhttp = checkhttp;
exports.get_g_index_url = get_g_index_url;
exports.configDir = configDir;
exports.getLanguage = getLanguage;
